package com.example.video

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.base.mvvm.BaseActivity
import com.example.video.adapter.GoodsAdapter
import com.example.video.databinding.ActivityCateXqBinding
@Route(path = "/app/CateXqActivity")
class CateXqActivity : BaseActivity<ActivityCateXqBinding,CateXqViewModel>() {
    @Autowired
    @JvmField
    var cid= 0
    @Autowired
    @JvmField
    var cname = ""

    lateinit var goodsAdapter: GoodsAdapter
    var priceOrder = ""

    override fun initView() {
        ARouter.getInstance().inject(this)
        binding.cateName.text = cname

        goodsAdapter = GoodsAdapter()
        binding.rv.adapter = goodsAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)

        viewModel.cateXq(cid,priceOrder,10,1)

        binding.priceOrder.setOnClickListener {
            if(priceOrder==""){
                priceOrder = "asc"
                binding.priceOrder.text = "价格排序-升"
            }else if(priceOrder=="asc"){
                priceOrder = "desc"
                binding.priceOrder.text = "价格排序-降"
            }else {
                priceOrder = ""
                binding.priceOrder.text = "价格排序"
            }
            //如果有销量排序同时存在，一定将销量排序给出“”
            viewModel.cateXq(cid,priceOrder,10,1)
        }

        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/DetailActivity").withInt("goodsId",goodsAdapter.data[position].id).navigation()
        }
    }

    override fun initData() {
        viewModel.goodsSuccess.observe(this){
            if(it.code==200){
                goodsAdapter.data.clear()
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<CateXqViewModel> {
       return CateXqViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_cate_xq
    }

}